function imageUpload(tag) {
  $('#ad-resource' + tag).fileupload({
    dataType: 'json',
    add: function (e, data) {
      $('#progress-bar' + tag).css('width', '0%');
      $('#progress' + tag).show();
      data.submit();
    },
    progressall: function (e, data) {
      var progress = parseInt(data.loaded / data.total * 100, 10) + '%';
      $('#progress-bar' + tag).css('width', progress);
    },
    done: function (e, data) {
      if (data.result.state == "SUCCESS" || data.result.state == "EXISTS") {
        $("#ad-resource-img" + tag).attr("src", data.result.url).show();
        $("#input-ad-resource" + tag).val(data.result.title).blur();
        $("#control-resources" + tag).removeClass("has-error has-warning").addClass("has-success");
        $("#control-resources" + tag + " .help-block").html("图片上传成功！");
      } else {
        $("#ad-resource-img" + tag).attr("src", "#").hide();
        $("#control-resources" + tag).removeClass("has-success has-warning").addClass("has-error");
        $("#control-resources" + tag + " .help-block").html(data.result.state);
      }
      $('#progress' + tag).fadeOut();
    },
    error: function (e, data) {
      $("#carouse-img" + tag).attr("src", "#").hide();
      $("#control-resources" + tag).removeClass("has-success has-warning").addClass("has-error");
      $("#control-resources" + tag + " .help-block").html("图片上传失败！");
      $('#progress' + tag).fadeOut();
    }
  });
}

function adControlDeploySuccess() {
  $('#ad-submit-deploy').removeClass('btn-primary').attr('disabled', 'disabled');
  $('#ad-submit-play').addClass('btn-success').removeAttr('disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlDeployFailure() {
  $('#ad-submit-deploy').addClass('btn-primary').removeAttr('disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlPlaySuccess() {
  $('#ad-submit-play').removeClass('btn-success').attr('disabled', 'disabled');
  $('#ad-submit-stop').addClass('btn-warning').removeAttr('disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlPlayFailure() {
  $('#ad-submit-play').addClass('btn-success').removeAttr('disabled');
  $('#ad-submit-stop').removeClass('btn-warning').attr('disabled', 'disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlStopSuccess() {
  $('#ad-submit-stop').removeClass('btn-warning').attr('disabled', 'disabled');
  $('#ad-submit-cancel').addClass('btn-danger').removeAttr('disabled');
  $('#ad-submit-play').addClass('btn-success').removeAttr('disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlStopFailure() {
  $('#ad-submit-stop').addClass('btn-warning').remove('disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlCancelSuccess() {
  $('#ad-submit-cancel').removeClass('btn-danger').attr('disabled', 'disabled');
  $('#ad-submit-deploy').addClass('btn-primary').removeAttr('disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlCancelFailure() {
  $('#ad-submit-cancel').addClass('btn-danger').removeAttr('disabled');
  formInfo("#control-play-control-actions");
  $('#ad-control-spinner').hide();
}

function adControlSpinner() {
  $('#ad-control-spinner').show();
}

$(function () {
  $('tab-play-time a[href="#tab-continuous"]').click(function (e) {
    $('#play-time-submit').val('0');
    $(this).tab('show');
  });
  $('tab-play-time a[href="#tab-repeat"]').click(function (e) {
    $('#play-time-submit').val('1');
    $(this).tab('show');
  });
});

